package namespace.jcu.mobile.library.system;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View.OnClickListener;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseUser;


public class AddNewsActivity extends Activity {
	
	EditText title;
	EditText description;
	Button insertNews;
	Button resetText;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		Parse.initialize(this, "1GlGZQ1XWU34mHgFyDHZiZRr6HTTSWOZo2nSAowy", "GKVclKTd6TIPxLVPPbDiyvzAMoWA2XLf4ZdZYFwI");
		super.onCreate(savedInstanceState);
		
		
		//get user session
        ParseUser currentUser = ParseUser.getCurrentUser();
        
        //condition when user is already logged in
        if (currentUser != null)
        {
        	if (currentUser.getBoolean("Admin_Status") == true)		//check whether it's admin
        	{
                setContentView(R.layout.insert_news);

        		title = (EditText) findViewById(R.id.textTitle);
    	        description = (EditText) findViewById(R.id.textDesc);
    	   
    			insertNews = (Button) findViewById(R.id.insertNewsBtn);
    			resetText = (Button) findViewById(R.id.reset);
        		
    			//click on "Add into database" button
    			insertNews.setOnClickListener(new OnClickListener() {
    	        	
    	        	public void onClick(View v) {
    	        		
    	        		//create connection with parse "News" table
    	        		ParseObject newsTable = new ParseObject("News");
    	        		
    	        		//inserting data
    	        		newsTable.put("Title", title.getText().toString());
    	        		newsTable.put("Description", description.getText().toString());
    	        		
    	        		//commit the above query
    	        		newsTable.saveInBackground();

    	        		
    	        		//textfield reset
    	        		title.setText("");
    	        		description.setText("");
    	        		
    	          		Context context = getApplicationContext();
    	          		CharSequence text = "News Post has been updated.";
    	          		int duration = Toast.LENGTH_LONG;

    	          		
    	          		//toast message to display when the item is clicked
    	          		Toast toast = Toast.makeText(context, text, duration);
    	          		toast.show();
    	        	}
    	        });
        			
        			
    			//button click on reset button
        			resetText.setOnClickListener(new OnClickListener() {
        				public void onClick(View v) {
        					//erasing all the text inside textfields
        					title.setText("");
        					description.setText("");
        				}
        			});
        			
    		}
        	else
        	{
          		Context context = getApplicationContext();
          		CharSequence text = "Sorry, this feature's only available for Administrator.";
          		int duration = Toast.LENGTH_LONG;

          		
          		//toast message to display when the item is clicked
          		Toast toast = Toast.makeText(context, text, duration);
          		toast.show();
          		
          		//call home page class
				Intent i = new Intent(AddNewsActivity.this, HomeActivity.class);
				startActivity(i);
        	}
    	}
        else
        {
      		Context context = getApplicationContext();
      		CharSequence text = "Please Log in to add news.";
      		int duration = Toast.LENGTH_LONG;

      		
      		//toast message to display when the item is clicked
      		Toast toast = Toast.makeText(context, text, duration);
      		toast.show();
        	
      		//call login page class
			Intent i = new Intent(AddNewsActivity.this, LoginActivity.class);
			startActivity(i);
        }
    }
    
    
   
	
}
